﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataSettings.Providers
{
    public class DataProvider
    {
        #region Constructor(s)
        public DataProvider()
        {
            Connection = new SqlConnection(_GetStringConnection());
        }
        #endregion

        #region Attributes
        public SqlConnection Connection;
        #endregion

        #region Methods
        #region Privates
        private string _GetStringConnection()
        {
            return @"Data Source=NELSON-PC\SQLEXPRESS;Initial Catalog=apvdp;Integrated Security=True";
        }
        #endregion
        #region Publics
        public SqlCommand CreateCommand()
        {
            return Connection.CreateCommand();
        }
        public int ExecuteWithReturnValue(SqlCommand command)
        {
            SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
            returnValue.Direction = ParameterDirection.Output;

            command.Parameters.Add(returnValue);

            Connection.Open();

            command.ExecuteNonQuery();

            Connection.Close();

            return (int)returnValue.Value;
        }

        public void ExecuteNonQuery(SqlCommand command)
        {
            Connection.Open();

            command.ExecuteNonQuery();

            Connection.Close();
        }

        public DataTable GetTabelaCorrespondente(SqlCommand command)
        {
            Connection.Open();

            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            da.Fill(ds);

            Connection.Close();

            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            return dt;
        }

        //public List<DadosParaGridView> ReturnData(SqlCommand command)
        //{
        //    List<DadosParaGridView> lista = new List<DadosParaGridView>();

        //    Connection.Open();

        //    SqlDataReader reader = command.ExecuteReader();
        //    while (reader.Read())
        //    {
        //        string statusPag = string.Empty;

        //        string nome = reader["Nome"].ToString();
        //        string turma = reader["Turma"].ToString();
        //        if (Convert.ToBoolean(reader["Status"].ToString()))
        //        {
        //            statusPag = "Pago";
        //        }
        //        else
        //        {
        //            statusPag = "Não Pago";
        //        }

        //        DadosParaGridView dados = new DadosParaGridView(nome, turma, statusPag);
        //        lista.Add(dados);
        //    }

        //    Connection.Close();

        //    return lista;
        //}

        //public DadosEvento[] ReturnEventList(SqlCommand command)
        //{
        //    List<DadosEvento> listaDadosEvento = new List<DadosEvento>();
        //    DadosEvento dadosEvento = null;

        //    Connection.Open();

        //    SqlDataReader reader = command.ExecuteReader();
        //    while (reader.Read())
        //    {
        //        int eventoID = int.Parse(reader["EventoID"].ToString());
        //        string nomeEvento = reader["NomeEvento"].ToString();
        //        string dataEvento = reader["DataEvento"].ToString();

        //        dadosEvento = new DadosEvento(eventoID, nomeEvento, dataEvento);
        //        listaDadosEvento.Add(dadosEvento);
        //    }

        //    DadosEvento[] eventList = new DadosEvento[listaDadosEvento.Count];

        //    for (int i = 0; i < listaDadosEvento.Count; i++)
        //    {
        //        DateTime dateTimeEvent = DateTime.Parse(listaDadosEvento[i].DataEvento);
                
        //        if(dateTimeEvent >= DateTime.Now)
        //            eventList[i] = listaDadosEvento[i];
        //    }

        //    Connection.Close();

        //    return eventList;
        //}
        #endregion
        #endregion
    }
}
